﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DXApplicationTangche.UC.订单一览新.辅料.辅料管理;
using DXApplicationTangche.UC.订单一览新.辅料.辅料重构;
using DXApplicationTangche.UC.货号管理.service;
using DXApplicationTangche.UC.金蝶.dto;
using DXApplicationTangche.UC.面料审核.Service;
using Kingdee.BOS.WebApi.Client;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.金蝶
{
    public partial class Form金蝶EBOM修改 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        //  辅料
        private List<SubMaterialSpuDao> subMaterialSpuDaos = new List<SubMaterialSpuDao>();
        private SubMaterialSpuDao subMaterialSpuDao = new SubMaterialSpuDao();
        private List<SubMaterialSkuDTO> subMaterialSkuDtos = new List<SubMaterialSkuDTO>();
        //
        private AccessoryKingDeeVO accessoryKingDeeVO选择辅料 = new AccessoryKingDeeVO();
        private List<AccessoryKingDeeVO> accessoryKingDee已选辅料list = new List<AccessoryKingDeeVO>();
        public String styleSizeCd { get; set; }
        public String styleNameCn { get; set; }
        public String materialCode { get; set; }
        private String fNumber面料 { get; set; }

        private List<BOMListDTO> bOMListDTOs一览 = new List<BOMListDTO>();

        public Form金蝶EBOM修改()
        {
            InitializeComponent();
        }
        public Form金蝶EBOM修改(String styleNameCN, String materialNo)
        {
            InitializeComponent();
            this.styleNameCn = styleNameCN;
            this.materialCode = materialNo;
            this.styleSizeCd = "IGS_GROUP_SIZE-M";

            this.textBox款式名称.Text = this.styleNameCn;
            this.textBox面料号.Text = this.materialCode;
            //  已创建尺寸显示
            List<KingDeeEBOMResultDTO> kingDeeEBOMResultDTOs = 货号管理Service.getEBOMAll(this.styleNameCn, this.materialCode);
            this.gridControl已创建尺寸.DataSource = kingDeeEBOMResultDTOs;
            //  面料
            KingDeeMaterialIntoDTO kingDeeMaterialIntoDTO = 货号管理Service.getKingDeeMaterialIntoDTO(materialNo);
            this.fNumber面料 = kingDeeMaterialIntoDTO.fnumber;
            //  辅料
            //this.辅料一览 = 辅料Service.getAccessoryWithKingDeeInfoAll();
            //this.searchLookUpEdit辅料一览.Properties.DataSource = this.辅料一览;
            this.subMaterialSpuDaos = 辅料Service.getSubMaterialSpuAll();
            this.gridControl辅料SPU一览.DataSource = this.subMaterialSpuDaos;
        }

        private void 控件控制()
        {
            this.textBox面料固定损耗.Enabled = true;
            this.textBox数量.Enabled = true;
            this.simpleButton添加辅料.Enabled = true;
            this.simpleButton保存BOM.Enabled = true;
        }

        private void simpleButton进入可修改状态_Click(object sender, EventArgs e)
        {
            this.控件控制();
        }

        #region
        //private void searchLookUpEdit辅料一览_Popup(object sender, EventArgs e)
        //{
        //    PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
        //    SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
        //    LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
        //    //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
        //    if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
        //    {
        //        //得到空的空间
        //        EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

        //        //得到取消按钮，重写点击事件
        //        Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
        //        LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
        //        clearBtn.Click += clear辅料一览Btn_Click;

        //        //添加一个simplebutton控件(确认按钮)
        //        LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
        //        myLCI.TextVisible = false;
        //    }
        //}
        //private void searchLookUpEdit辅料一览View_Click(object sender, EventArgs e)
        //{
        //    int[] a = this.searchLookUpEdit辅料一览.Properties.View.GetSelectedRows();
        //    if (a.Length > 0)
        //    {
        //        foreach (int rowHandle in a)
        //        {
        //            String fNumber = this.searchLookUpEdit辅料一览.Properties.View.GetRowCellValue(rowHandle, "fNumber").ToString();
        //            this.取单条数据(fNumber);
        //        }
        //    }
        //}
        //private void 取单条数据(String fNumber)
        //{
        //    foreach (AccessoryKingDeeDTO item in this.辅料一览)
        //    {
        //        if (fNumber.Equals(item.fNumber))
        //        {
        //            this.accessoryKingDeeVO选择辅料 = new AccessoryKingDeeVO(item);
        //            break;
        //        }
        //    }
        //}
        ///// <summary>
        ///// 清除按钮事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void clear辅料一览Btn_Click(object sender, EventArgs e)
        //{
        //    this.searchLookUpEdit辅料一览.ToolTip = null;
        //    searchLookUpEdit辅料一览.EditValue = null;
        //}
        //private void searchLookUpEdit辅料一览_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        //{
        //    if (null != e.Value)
        //    {
        //        e.DisplayText = this.accessoryKingDeeVO选择辅料.subMaterialName;
        //    }
        //}
        //private void comboBox尺码组_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    this.styleSizeCd = "IGS_GROUP_SIZE-M";
        //}
        #endregion

        private void simpleButton添加辅料_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(this.accessoryKingDeeVO选择辅料.fNumber))
            {
                if (!String.IsNullOrWhiteSpace(this.textBox数量.Text))
                {
                    try
                    {
                        this.accessoryKingDeeVO选择辅料.buildFFIXSCRAPQTY(Convert.ToDouble(this.textBox数量.Text.Trim()));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        MessageBox.Show("请输入正确格式辅料固定损耗");
                        this.ActiveControl = this.textBox数量;
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("请输入该辅料的固定损耗");
                    return;
                }
                if (this.accessoryKingDee已选辅料list.Count > 0)
                {
                    if (!this.accessoryKingDee已选辅料list.Exists(dto => dto.fNumber.Equals(this.accessoryKingDeeVO选择辅料.fNumber)))
                    {
                        this.accessoryKingDee已选辅料list.Add(this.accessoryKingDeeVO选择辅料);
                    }
                }
                else
                {
                    this.accessoryKingDee已选辅料list.Add(this.accessoryKingDeeVO选择辅料);
                }
            }
            //foreach (AccessoryKingDeeVO accessoryKingDeeVO in this.accessoryKingDee已选辅料list)
            //{
            //    this.bOMListDTOs一览.Add(new BOMListDTO(this.styleSizeCd, accessoryKingDeeVO));
            //}
            //this.gridControl1.DataSource = this.bOMListDTOs一览;
            //this.gridView1.RefreshData();
            this.gridControl子项物料一览.DataSource = this.accessoryKingDee已选辅料list;
            this.gridView子项物料一览.RefreshData();
            this.gridView子项物料一览.BestFitColumns();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String subMaterialCode = FunctionHelper.GetgridViewValue(
                                        this.gridView子项物料一览,
                                        this.gridView子项物料一览.FocusedRowHandle,
                                        "subMaterialCode");
                for (int i = this.accessoryKingDee已选辅料list.Count - 1; i >= 0; i--)
                {
                    if (subMaterialCode.Equals(this.accessoryKingDee已选辅料list[i].subMaterialCode))
                    {
                        this.accessoryKingDee已选辅料list.Remove(this.accessoryKingDee已选辅料list[i]);
                        break;
                    }
                }
                this.gridView子项物料一览.RefreshData();
            }
            catch { }
        }

        private void simpleButton保存BOM_Click(object sender, EventArgs e)
        {
            List<ChildDTO> childDTOs = new List<ChildDTO>();
            if (!String.IsNullOrWhiteSpace(this.textBox面料固定损耗.Text))
            {
                try
                {
                    childDTOs.Add(new ChildDTO(this.fNumber面料, Convert.ToDouble(this.textBox面料固定损耗.Text.Trim())));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    MessageBox.Show("请输入正确格式面料固定损耗");
                    this.ActiveControl = this.textBox面料固定损耗;
                    return;
                }
            }
            else
            {
                MessageBox.Show("请填写面料固定损耗");
                return;
            }
            //  辅料损耗
            foreach (AccessoryKingDeeVO item in this.accessoryKingDee已选辅料list)
            {
                childDTOs.Add(new ChildDTO(item.fNumber, item.FFIXSCRAPQTY));
            }
            //foreach (BOMListDTO bOMListDTO in this.bOMListDTOs一览)
            //{
            //    childDTOs.Add(new ChildDTO(bOMListDTO.fnumber, bOMListDTO.quantity));
            //}
            //保存
            // 根据金蝶EBOM保存返回值IsSuccess判断弹框显示
            //KindDeeApplyReturnDTO result = 货号管理Service.金蝶EBOM保存(this.styleNameCn, this.materialCode, this.styleSizeCd, childDTOs);
            //if (!result.IsSuccess)
            //{
            //    MessageBox.Show(result.SuccessMessages);
            //    return;
            //}
            String id = Regex.Replace(this.styleNameCn, " .*", "") + "_" + this.materialCode;
            货号管理Service.buildSaleSampleCreateBom(id);
            MessageBox.Show("操作完成");
            this.bOMListDTOs一览.Clear();
        }

        private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumn尺码"))
            {
                if (!String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    try { e.DisplayText = e.DisplayText.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
                else
                {
                    e.DisplayText = "样衣无尺寸定义";
                }
            }
        }

        private void gridView辅料SPU一览_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                String subMaterialCode = FunctionHelper.GetgridViewValue(
                                        this.gridView辅料SPU一览,
                                        this.gridView辅料SPU一览.FocusedRowHandle,
                                        "subMaterialCode");
                foreach (SubMaterialSpuDao item in this.subMaterialSpuDaos)
                {
                    if (subMaterialCode.Equals(item.subMaterialCode))
                    {
                        this.subMaterialSpuDao = item;
                        break;
                    }
                }
                this.subMaterialSkuDtos = 辅料Service.getSubMaterialSkuListBySubMaterialCode(subMaterialCode);
                this.gridControl辅料SKU.DataSource = this.subMaterialSkuDtos;
                this.gridView辅料SKU.RefreshData();
                this.textBox当前选择辅料.Clear();
            }
            catch { }
        }

        private void gridView2_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                String id = FunctionHelper.GetgridViewValue(
                                        this.gridView辅料SKU,
                                        this.gridView辅料SKU.FocusedRowHandle,
                                        "id");
                foreach (SubMaterialSkuDTO item in this.subMaterialSkuDtos)
                {
                    if (id.Equals(item.id))
                    {
                        this.textBox当前选择辅料.Text = item.subMaterialCode + item.subMaterialSpec;
                        this.accessoryKingDeeVO选择辅料 = new AccessoryKingDeeVO(this.subMaterialSpuDao, item);
                        break;
                    }
                }
            }
            catch { }
        }

        private void 维护辅料规格ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.subMaterialSpuDao.subMaterialCode))
            {
                MessageBox.Show("请先选择要维护的辅料");
                return;
            }
            new Form辅料SKU维护(
                this.subMaterialSpuDao.subMaterialTypeId,
                this.subMaterialSpuDao.subMaterialType,
                this.subMaterialSpuDao.subMaterialCode,
                this.subMaterialSpuDao.subMaterialName,
                this.subMaterialSpuDao.subMaterialSupplier
                ).ShowDialog();
            this.subMaterialSkuDtos = 辅料Service.getSubMaterialSkuListBySubMaterialCode(this.subMaterialSpuDao.subMaterialCode);
            this.gridControl辅料SKU.DataSource = this.subMaterialSkuDtos;
            this.gridView辅料SKU.RefreshData();
            this.textBox当前选择辅料.Clear();
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new Form辅料重构().ShowDialog();
            this.subMaterialSpuDaos = 辅料Service.getSubMaterialSpuAll();
            this.gridControl辅料SPU一览.DataSource = this.subMaterialSpuDaos;
        }
    }
}
